package controllers

import (
	"github.com/astaxie/beego"
	"map/models"
)

type BaseController struct {
	beego.Controller
	admin   *models.Admin
	isLogin bool
}

func (this *BaseController) Prepare() {
	v := this.GetSession("admin")
	if v != nil {
		admin, err := models.AdminById(v.(int))
		if err == nil {
			this.admin = admin
			this.isLogin = true
		}
	}
}

func (this *BaseController) success(msg string) {
	out := make(map[string]interface{})
	out["status"] = 0
	out["message"] = msg
	this.Data["json"] = out
	this.ServeJSON()
	this.StopRun()
}
func (this *BaseController) errors(msg string, status int) {
	out := make(map[string]interface{})
	out["status"] = status
	out["message"] = msg
	this.Data["json"] = out
	this.ServeJSON()
	this.StopRun()
}

func (this *BaseController) successData(msg string, data interface{}) {
	out := make(map[string]interface{})
	out["status"] = 0
	out["message"] = msg
	out["data"] = data
	this.Data["json"] = out
	this.ServeJSON()
	this.StopRun()
}
